﻿using System;
using System.Data.SqlClient;
namespace Helpers.Common
{
    public class SqlResponse<DataType> : Response<DataType>
    {
        public SqlResponse()
            : base() { }
        public SqlResponse(string message)
            : base(message) { }
        public SqlResponse(int errorCode)
            : base(errorCode) { }
        public SqlResponse(DataType data)
            : base(data) { }
        public SqlResponse(bool success, string message)
            : base(success, message) { }
        public SqlResponse(bool success, string message, int errorCode)
            : base(success, message, errorCode) { }
        public SqlResponse(bool success, string message, DataType data)
            : base(success, message, data) { }
        public SqlResponse(bool success, string message, int errorCode, DataType data)
            : base(success, message, errorCode, data) { }
        public SqlResponse(SqlException ex)
            : base(ex) { }

        #region Overrided Methods

        protected override void FillWithException(Exception ex)
        {
            if (ex is SqlException)
            {
                SqlException sqlEx = (SqlException)ex;

                if (sqlEx.Errors.Count > 0)
                {
                    SqlError error = sqlEx.Errors[0];

                    this.Message = error.Message;
                    this.ErrorCode = error.Number;
                }
            }

            base.FillWithException(ex);
        }

        #endregion
    }

    public class SqlResponse : Response
    {
        public SqlResponse()
            : base() { }
        public SqlResponse(string message)
            : base(message) { }
        public SqlResponse(int errorCode)
            : base(errorCode) { }
        public SqlResponse(object data)
            : base(data) { }
        public SqlResponse(bool success, string message)
            : base(success, message) { }
        public SqlResponse(bool success, string message, int errorCode)
            : base(success, message, errorCode) { }
        public SqlResponse(bool success, string message, object data)
            : base(success, message, data) { }
        public SqlResponse(bool success, string message, int errorCode, object data)
            : base(success, message, errorCode, data) { }
        public SqlResponse(SqlException ex)
            : base(ex) { }

        #region Overrided Methods

        protected override void FillWithException(Exception ex)
        {
            if (ex is SqlException)
            {
                SqlException sqlEx = (SqlException)ex;

                if (sqlEx.Errors.Count > 0)
                {
                    SqlError error = sqlEx.Errors[0];

                    this.ErrorCode = error.Number;

                    if (error.Number > 50000)
                        this.Message = error.Message;
                }
            }

            base.FillWithException(ex);
        }

        #endregion
    }
}